package com.msf.modules.sys.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.msf.common.constant.CacheConstant;
import com.msf.common.domain.vo.Result;
import com.msf.common.utils.oConvertUtils;
import com.msf.modules.sys.domain.vo.DepartIdModel;
import com.msf.modules.sys.domain.vo.SysDepartTreeModel;
import com.msf.modules.sys.entity.SysDepart;
import com.msf.modules.sys.service.ISysDepartService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 组织机构表 前端控制器
 * </p>
 *
 * @author author
 * @since 2024-10-09
 */
@Slf4j
@Api(tags = "部门管理")
@RestController
@RequestMapping("/sys/depart")
@RequiredArgsConstructor
public class SysDepartController {

    private final ISysDepartService sysDepartService;

    /**
     * 查询数据 查出所有部门,并以树结构数据格式响应给前端
     */
    @ApiOperation(value = "查出所有部门")
    @RequestMapping(value = "/queryTreeList", method = RequestMethod.GET)
    public Result<List<SysDepartTreeModel>> queryTreeList(@RequestParam(name = "keywords", required = false) String keywords) {
        return sysDepartService.queryTreeList(keywords);
    }

    /**
     * 异步查询部门list
     * @param parentId   父节点 异步加载时传递
     * @param ids        前端回显是传递
     * @param primaryKey 主键字段（id或者orgCode）
     */
    @ApiOperation(value = "异步查询部门list")
    @RequestMapping(value = "/queryDepartTreeSync", method = RequestMethod.GET)
    public Result<List<SysDepartTreeModel>> queryDepartTreeSync(@RequestParam(name = "pid", required = false) String parentId, @RequestParam(name = "ids", required = false) String ids, @RequestParam(name = "primaryKey", required = false) String primaryKey) {
        return sysDepartService.queryDepartTreeSync(parentId, ids, primaryKey);
    }

    /**
     * 获取某个部门的所有父级部门的ID
     * @param departId 根据departId查
     * @param orgCode  根据orgCode查，departId和orgCode必须有一个不为空
     */
    @ApiOperation(value = "获取某个部门的所有父级部门的ID")
    @GetMapping("/queryAllParentId")
    public Result<?> queryParentIds(
            @RequestParam(name = "departId", required = false) String departId,
            @RequestParam(name = "orgCode", required = false) String orgCode) {
        return sysDepartService.queryParentIds(departId,orgCode);
    }

    /**
     * 添加新数据 添加用户新建的部门对象数据,并保存到数据库
     */
    @ApiOperation(value = "添加部门")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public Result<SysDepart> add(SysDepart sysDepart, HttpServletRequest request) {
        return sysDepartService.add(sysDepart, request);
    }
    /**
     * 编辑数据 编辑部门的部分数据,并保存到数据库
     *
     * @param sysDepart
     * @return
     */
    @ApiOperation(value = "修改部门")
    @RequestMapping(value = "/edit", method = {RequestMethod.PUT, RequestMethod.POST})
    public Result<SysDepart> edit(SysDepart sysDepart, HttpServletRequest request) {
        return sysDepartService.edit(sysDepart,request);
    }

    /**
     * 通过id删除
     *
     * @param id
     */
    @ApiOperation(value = "删除部门")
    @RequestMapping(value = "/delete", method = RequestMethod.DELETE)
    public Result<?> delete(@RequestParam(name = "id", required = true) String id) {
        return sysDepartService.delete(id);
    }


    /**
     * 批量删除 根据前端请求的多个ID,对数据库执行删除相关部门数据的操作
     *
     * @param ids
     * @return
     */
    @ApiOperation(value = "批量删除")
    @RequestMapping(value = "/deleteBatch", method = RequestMethod.DELETE)
    @CacheEvict(value = {CacheConstant.SYS_DEPARTS_CACHE, CacheConstant.SYS_DEPART_IDS_CACHE}, allEntries = true)
    public Result<?> deleteBatch(@RequestParam(name = "ids", required = true) String ids) {
        return sysDepartService.deleteBatch(ids);
    }

    /**
     * 查询数据 添加或编辑页面对该方法发起请求,以树结构形式加载所有部门的名称,方便用户的操作
     */
    @RequestMapping(value = "/queryIdTree", method = RequestMethod.GET)
    public Result<List<DepartIdModel>> queryIdTree() {
        return sysDepartService.queryIdTree();
    }

    /**
     * 查询所有部门信息
     *
     * @return
     */
    @ApiOperation(value = "查询所有部门列表信息")
    @GetMapping("listAll")
    public Result<List<SysDepart>> listAll(@RequestParam(name = "keywords", required = false) String keywords) {
        Result<List<SysDepart>> result = new Result<>();
        LambdaQueryWrapper<SysDepart> query = new LambdaQueryWrapper<SysDepart>();
        query.orderByAsc(SysDepart::getOrgCode);
        if (oConvertUtils.isNotEmpty(keywords)) {
            query.and(e -> e.like(SysDepart::getOrgCode, keywords).or().like(SysDepart::getDepartName, keywords));
        }
        List<SysDepart> ls = sysDepartService.list(query);
        result.setSuccess(true);
        result.setResult(ls);
        return result;
    }

}
